package com.dean.pokeking.ui.pages

import android.graphics.BitmapFactory
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ComposeCompilerApi
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.dean.pokeking.beans.MonsterButton
import com.dean.pokeking.ui.MainViewModel

/**
 * @ClassName TODO
 * @Description TODO
 * @Author Dean
 * @Date 2023/3/2
 * @Version 1.0
 */

@Composable
fun MonstersChoosePage(
    modifier: Modifier = Modifier,
    onClick: (String) -> Unit
) {
    val viewModel = viewModel<MainViewModel>()
    val state = viewModel.mainState.collectAsState().value
    val currentNpc = state.currentNpc
    val monsterList = viewModel.getNpcMonsterButtonListByNpcName(currentNpc)
        .collectAsState(emptyList()).value
    LazyVerticalGrid(
        modifier = modifier.fillMaxSize(),
        columns = GridCells.Fixed(3),
        contentPadding = PaddingValues(10.dp)
    ) {
        items(items = monsterList, key = { it.id }) {
            MonsterButtonCell(monsterButton = it) { onClick(it.monsterName) }
        }
    }
}

@Composable
fun MonsterButtonCell(
    monsterButton: MonsterButton,
    onClick: () -> Unit
) {
    Button(
        modifier = Modifier
            .size(130.dp)
            .padding(5.dp),
        onClick = onClick,
    ) {
        Box(modifier = Modifier.fillMaxSize()) {
            val bitmap = runCatching {
                val file =
                    LocalContext.current.assets.open("monsters/${monsterButton.monsterName}.png")
                BitmapFactory.decodeStream(file).asImageBitmap()
            }.getOrNull()
            bitmap?.let { Image(bitmap = bitmap, contentDescription = "") }
            Text(
                modifier = Modifier
                    .background(Color.White.copy(alpha = 0.7f))
                    .align(Alignment.TopStart),
                text = monsterButton.monsterName,
                fontSize = 20.sp
            )
            Text(
                modifier = Modifier.align(Alignment.BottomEnd),
                text = "队伍: ${monsterButton.monsterNumber}",
                fontSize = 18.sp
            )
        }
    }
}

@Composable
@Preview(showBackground = true)
fun ButtonPreview() {
    MonsterButtonCell(monsterButton = MonsterButton(id = 0, monsterName = "自爆磁怪", monsterNumber = 5)) {
        
    }
}